<template>
  <div class="editor-page">
    <div class="container page">
      <div class="row">
        <div class="col-md-10 offset-md-1 col-xs-12">
          <form>
            <fieldset>
              <fieldset class="form-group">
                <input
                  v-model="title"
                  required
                  type="text"
                  class="form-control form-control-lg"
                  placeholder="Article Title"
                />
              </fieldset>
              <fieldset class="form-group">
                <input
                  v-model="description"
                  required
                  type="text"
                  class="form-control"
                  placeholder="What's this article about?"
                />
              </fieldset>
              <fieldset class="form-group">
                <textarea
                  v-model="body"
                  required
                  class="form-control"
                  rows="8"
                  placeholder="Write your article (in markdown)"
                ></textarea>
              </fieldset>
              <fieldset class="form-group">
                <input
                  v-model="tagList"
                  required
                  type="text"
                  class="form-control"
                  placeholder="Enter tags"
                />
                <div class="tag-list"></div>
              </fieldset>
              <button
                @click="onPublish"
                :disabled="publishDisabled || !formValidate"
                class="btn btn-lg pull-xs-right btn-primary"
                :class="{ disabled: publishDisabled || !formValidate }"
                type="button"
              >Publish Article</button>
            </fieldset>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { addArticle } from "@/api/article";

export default {
  middleware: "authenticated",
  data() {
    return {
      title: '',
      description: '',
      body: '',
      tagList: '',
      publishDisabled: false,
    };
  },
  methods: {
    async onPublish() {
      this.publishDisabled = true;
      await addArticle({
        title: this.title,
        description: this.description,
        body: this.body,
        tagList: this.tagList.split(','),
      })
      this.publishDisabled = false;
    },
  },
  computed: {
    formValidate() {
      return this.title && this.description && this.body && this.tagList
    }
  }
};
</script>

<style>
</style>